
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.management.MBeanAttributeInfo;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class ClientInfo extends JFrame{
    String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    final DateFormat timeFormat = new SimpleDateFormat("HH : mm : ss");
    JFrame frame = new JFrame("MemberShipRelation");
    JDesktopPane pane = new JDesktopPane();
    JPanel p0 = new JPanel();
    JLabel lbl0 = new JLabel();
    JLabel lbl1 = new JLabel();
    JLabel lbl2 = new JLabel();
    JLabel lbdate = new JLabel();
    JLabel lbtime = new JLabel();
    
    JLabel lbempid=new JLabel();
    JLabel vlempid=new JLabel();
    
    JLabel lbname=new JLabel();
    JLabel vlname=new JLabel();
    
    JLabel lbagent = new JLabel();
    JTextField agnttxt = new JTextField();
  
    JButton save = new JButton("SAVE");
    JButton exit = new JButton("EXIT");
    JButton clear = new JButton("CLEAR");
    JButton font = new JButton("Frontpage");
     AutoTextComplete agntact = new AutoTextComplete(agnttxt);
   ClientInfo(String id) throws Exception {
       
       try{
            DBManager db = null;
            Connection connection = db.openDBConnection();
            String sql12 = "Select * from CHAIN.registration where EMPID='"+ id+ "';";
            PreparedStatement ps = (PreparedStatement)connection.prepareStatement(sql12);
	     ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String empid=rs.getString(1);
                String name=rs.getString(2);
                 
           /*This add id Lable TextField*/
       
          pane.add(lbempid);
          lbempid.setBounds(70, 140, 200, 30);
          lbempid.setFont(new java.awt.Font("Trebuchet MS", 1, 18));
          lbempid.setText("Member_ID:");
          lbempid.setFont(new java.awt.Font("Trebuchet MS", 1, 18));
          pane.add(vlempid);
          vlempid.setBounds(205, 140, 200,30);
          vlempid.setText(empid);
          
           
        /*This add  name TextField*/
        
        pane.add(lbname);
        lbname.setBounds(70, 180, 200, 30);
        lbname.setFont(new java.awt.Font("Trebuchet MS", 1, 18));
        lbname.setText("Name:");
        pane.add(vlname);
        vlname.setBounds(205, 180, 200,30);
        vlname.setFont(new java.awt.Font("Trebuchet MS", 1, 18));
        vlname.setText(name);
        
       
                
           
            }
       }
        catch (Exception e5) {
        }
       
       try{
            DBManager db = null;
             Connection connection = db.openDBConnection();
              new AutoCompleteDBLink(agntact, "SELECT NAME FROM CHAIN.AGENT WHERE NAME NOT IN(SELECT AGNTNAME  FROM CHAIN.AGENT GROUP BY AGNTNAME HAVING ( COUNT(AGNTNAME) = 2));", connection);
            
           
           
       }
        catch (Exception e5) {
        }
        frame.setLocation(150, 30);
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.add(pane);
        pane.setBounds(0, 0, 500, 500);
        pane.setBackground(Color.pink);
        pane.add(lbl0);
        lbl0.setBounds(10, 10, 400, 40);
        lbl0.setText("Membership Relation");
        lbl0.setForeground(new java.awt.Color(0, 51, 255));
        lbl0.setFont(new java.awt.Font("Tempus Sans ITC", 3, 24));
        lbl0.setHorizontalAlignment(JLabel.CENTER);
        /* Display for Date */
        pane.add(lbdate);
        lbdate.setBounds(20, 50, 55, 20);
        lbdate.setText("Date:-");
        lbdate.setForeground(Color.YELLOW);
        lbdate.setFont(new Font("tohoma", Font.ITALIC, 14));
        pane.add(lbl1);
        lbl1.setBounds(70, 45, 100, 30);
        lbl1.setFont(new Font("tohoma", Font.ITALIC, 14));
        lbl1.setHorizontalAlignment(JLabel.LEFT);
        lbl1.setForeground(Color.yellow);
        lbl1.setText("" + date);
        /* Display for Time */
        pane.add(lbl2);
        lbl2.setBounds(20, 65, 150, 30);
        lbl2.setFont(new Font("tohoma ", Font.ITALIC, 14));
        lbl2.setHorizontalAlignment(JLabel.LEFT);
        final Date date = new Date();
        String time = timeFormat.format(date);
        lbl2.setText("Time - " + time);
        lbl2.setForeground(Color.yellow);
       
       
        /*This add Client Name And TextField*/
        
        pane.add(lbagent);
       lbagent.setBounds(70, 220, 200, 30);
       lbagent.setFont(new java.awt.Font("Trebuchet MS", 1, 18));
       lbagent.setText("Agent Name:");
       pane.add(agnttxt);
       agnttxt.setBounds(205, 220, 200,30);
       agnttxt.setFont(new java.awt.Font("Trebuchet MS", 1, 18));
    
 
        /*Add all Buttom To Frame*/
        
        pane.add(save, BorderLayout.NORTH);
        save.setBounds(20, 350, 100,30);
        
        
        pane.add(clear, BorderLayout.NORTH);
        clear.setBounds(140, 350, 100,30);
        
        
        pane.add(exit, BorderLayout.NORTH);
        exit.setBounds(260, 350, 100,30);
        
         
        pane.add(font,BorderLayout.NORTH);
        font.setBounds(380, 350, 100,30);
        
        
        /*Clear ActionListner Form*/
          clear.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
              
                agnttxt.setText("");
         
            }

          });
          /*Exit ActionListner Form*/
           exit.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
               frame.dispose();
            }
            
        });
            save.addActionListener(new ActionListener() {
            	   DBManager db = null;
                   Connection connection = db.openDBConnection();
                    
                      String empid=null;
           

            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    String empid=vlempid.getText();
                    String name=vlname.getText();
                    String agtname=agnttxt.getText();
                    String sql ="INSERT INTO CHAIN.AGENT(EMPID,NAME,AGNTNAME) VALUES ('"+empid+"','"+name+"','"+agtname+"')";
                     PreparedStatement ps = connection.prepareStatement(sql);
                     int n=ps.executeUpdate();
                     System.out.println("AGENT update is:"+n);
                    
                }
                catch(Exception e5)
                {
                    e5.printStackTrace();
                }
                String member=agnttxt.getText();
                try{
               
                String sql1= "DELETE FROM `CHAIN`.`AGENT` WHERE `AGNTNAME`='" + member + "' AND NAME='45';";
                PreparedStatement ps = (PreparedStatement) connection.prepareStatement(sql1);
                int n=ps.executeUpdate();
                System.out.println("dELETE dATA:"+n);
                
              // connection.close();
                }catch(Exception e1){}
           
                
               frame.dispose();
         
            }
        });
            
            
      
             /*Member Key Action Method*/
            
    
           }


}
